/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.EditorApplet;
import offset.nodes.client.editor.model.EditorDocument;
import offset.nodes.client.editor.model.messages.GetVirtualPageEditorInformation;
import offset.nodes.client.model.NodeTypes;
import offset.nodes.client.model.Server;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.virtual.model.GetSchemas;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeDefinition;
import offset.nodes.client.virtual.model.jcr.nodetype.NodeTypeReader;

/**
 *
 * @author Walter Lütgenau
 */
public class VirtualPageEditorApplet extends VirtualEditor {

    public static final String PROP_SAMPLE_HTML = "sampleHtml";

    URL service;
    URL repository;
    String instancePath;

    @Override
    public void initUI() {
        try {
            service = new URL(getParameter("service"));
            repository = new URL(getParameter("base"));
            instancePath = getParameter(VirtualEditorApplet.PAR_INSTANCE_PATH);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getInstancePath() {
        return instancePath;
    }

    public URL getService() {
        return service;
    }

    public URL getRepository() {
        return repository;
    }

    @Override
    public void startUI() {
        try {
            super.startUI();
            
            GetVirtualPageEditorInformation.Request request = new GetVirtualPageEditorInformation.Request();

            request.setInstancePath(getInstancePath());
            Server server = new ServerModel(getService());

            GetVirtualPageEditorInformation.Response response = (GetVirtualPageEditorInformation.Response) server.sendRequest(request);

            setMode(VirtualEditor.MODE_VIRTUAL_PAGE_EDITOR);
            ((EditorDocument) getBrowserPane().getDocument()).setBase(getRepository());
            getBrowserPane().setText(response.getSampleHtml());
            setupPane();

            getProperties().put(VirtualPageEditorApplet.PROP_SAMPLE_HTML, response.getSampleHtml());
            getProperties().put(Editor.PROP_TEMPLATE_NODE_PATH, response.getTemplateNodePath());
            getProperties().put(Editor.PROP_DOCUMENT_URL, repository.toString());
            if (response.getSecondaryQueries() != null)
                getProperties().put(Editor.PROP_SECONDARY_QUERIES, createSecondaryQueryMap(response.getSecondaryQueries()));
            else
                getProperties().put(Editor.PROP_SECONDARY_QUERIES, new HashMap<String, String>());
            if (response.getSelectQueries() != null)
                getProperties().put(Editor.PROP_SELECT_QUERIES, createSecondaryQueryMap(response.getSelectQueries()));
            else
                getProperties().put(Editor.PROP_SELECT_QUERIES, new HashMap<String, String>());
            getProperties().put(Editor.PROP_NODE_TYPES, getNodeTypes(server));
            getProperties().put(Editor.PROP_UPLOAD_TO, getService().toString());
            getProperties().put(PROP_INSTANCE_PATH, getInstancePath());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    protected HashMap<String, String> createSecondaryQueryMap(String[] queries) {
       HashMap<String, String> result = new HashMap<String, String>();

        if (queries != null)
            for (int i = 0; i < queries.length; i++) {
                if (queries[i] == null || queries[i].length() == 0)
                    continue;
                int start = queries[i].indexOf("=");
                if (start < 0) {
                    result.put(queries[i], "");
                    continue;
                }

                result.put(queries[i].substring(0, start), queries[i].substring(start+1));
            }

        return result;
    }
}
